from django.shortcuts import HttpResponse
from django.template.response import TemplateResponse
from .models import Activity
from django.contrib.auth.models import User

# Create your views here.
def takepartin(request,activity_id,username,op):
    if request.method == "GET":
        # 取出相应的活动
        activity = Activity.objects.get(activity_id = activity_id)
        # 取出相应的人员信息
        user = User.objects.get(username = username)
        if op == 'signup':
            # 判断是否报名
            if bool(activity.participants.filter(id = user.id))==True:
                message = {
                'message' : '--报名成功--'
                }
                return TemplateResponse(request,'activity/message.html',message)

            else:
                activity.participants.add(user)
                message = {
                'message' : '--报名成功--'
                }
                return TemplateResponse(request,'activity/message.html',message)
        elif op == 'cancel':
            # 取消报名
            # 判断是否报名
            if bool(activity.participants.filter(id = user.id))==True:
                activity.participants.remove(user)
                message = {
                'message' : '--取消报名成功--'
                }
                return TemplateResponse(request,'activity/message.html',message)

            else:
                message = {
                'message' : '--取消报名成功--'
                }
                return TemplateResponse(request,'activity/message.html',message)
        else:
            pass
    else:
        message = {
        'message' : '--Unknown Response!--'
        }
        return TemplateResponse(request,'activity/message.html',message)

